//
//  MBGoodsViewController.m
//  MemberBusiness-ios
//
//  Created by cherry pu on 2018/4/19.
//  Copyright © 2018年 zhangXin. All rights reserved.
//

#import "MBGoodsViewController.h"
#import "MBGoodsTypeView.h"
#import "MBGoodsTableViewCell.h"
#import "MBGoodsDetailViewController.h"

static NSString* GOODS_TABLE_CELL = @"GOODS_TABLE_CELL";

@interface MBGoodsViewController ()
@property (nonatomic,weak)MBGoodsTypeView* typeView;

@end

@implementation MBGoodsViewController
@dynamic viewModel;
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setupLayout];
}

-(void)setupLayout
{
    [super setupLayout];
    
    self.view.backgroundColor = COLOR_BG;
    
    self.naviView.titleLabel.text = @"商品管理";
    self.viewModel.is_on_sale = 1;
    MBGoodsTypeView* typeView = [[MBGoodsTypeView alloc]initWithTypeArray:@[@"上架商品",@"下架商品",@"违规商品"]];
    self.typeView = typeView;
    [self.view addSubview:typeView];
    
    [typeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(SCREEN_STATUS_BAR_HEIGHT+44);
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo([UIView lf_sizeFromIphone6:44]);
    }];
    
    [self setUpMjRefresh];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(typeView.mas_bottom);
        make.left.right.bottom.equalTo(self.view);
    }];

//    切换type
    @weakify(self);
    [RACObserve(typeView, type) subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        if ([x intValue] == 0) {
            self.viewModel.is_on_sale = 1;
        }else if ([x intValue] == 1){
            self.viewModel.is_on_sale = 0;
        }else {
            self.viewModel.is_on_sale = 2;
        }
        [self pullTableViewRequestData:YES]
        ;
    }];
    
    [RACObserve(self.viewModel, selectInfoModel) subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        MBGoodsInfoModel* infoModel = x;
        if (infoModel) {
            MBGoodsDetailViewController* goodsDetailVC = [[MBGoodsDetailViewController alloc]init];
            goodsDetailVC.infoModel = infoModel;
            [self.navigationController pushViewController:goodsDetailVC animated:YES];
        }
    }];
}

-(void)setUpData
{
    [self.viewModel sendRequest:^(bool success) {
        [self hideLoadingViewFooter];
        if (success) {
            [self.tableView reloadData];
        }
    }];
}

-(void)pullTableViewRequestData:(BOOL)isRefresh
{
    if (isRefresh) {
        [self.viewModel initRequestPullPage];
    }
    
    [self setUpData];
}

-(Class)cellClassForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [MBGoodsTableViewCell class];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MBBaseTableViewCell *cell = [[self cellClassForRowAtIndexPath:indexPath] cellForTableView:tableView viewModel:[self.viewModel cellViewModelForRowAtIndexPath:indexPath]];
    cell.selectionStyle = [self.viewModel tableViewCellSelectionStyle];
    
    @weakify(self);
    [[cell.viewModel.cellSelectedSignal takeUntil:cell.rac_prepareForReuseSignal]subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        [self.viewModel.dataArray removeObject:cell.viewModel];
        [self.tableView reloadData];
    }];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (self.typeView.type == 0) {
        MBGoodsTableViewCellModel* model = (MBGoodsTableViewCellModel*)[self.viewModel cellViewModelForRowAtIndexPath:indexPath];
        [self.viewModel toGoodsDetailVCWithViewModel:model];
    }else if (self.typeView.type == 1){
        [InterfaceHUD showErrorHUDWithString:@"商品已下架，无法查看"];
    }else {
        [InterfaceHUD showErrorHUDWithString:@"违规商品，无法查看"];
    }
}



@end
